import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:online_bookstore/constants/app_constants.dart';
import 'package:online_bookstore/providers/auth_provider.dart';
import 'package:online_bookstore/screens/home_screen.dart';
import 'package:online_bookstore/screens/auth/login_screen.dart';

class SplashScreen extends ConsumerStatefulWidget {
  const SplashScreen({super.key});

  @override
  ConsumerState<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends ConsumerState<SplashScreen> {
  @override
  void initState() {
    super.initState();
    _navigateToNextScreen();
  }

  Future<void> _navigateToNextScreen() async {
    await Future.delayed(const Duration(seconds: 3));
    
    if (mounted) {
      final authState = ref.read(authProvider);
      if (authState.isAuthenticated) {
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (context) => const HomeScreen()),
        );
      } else {
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (context) => const LoginScreen()),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.primary,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // App Logo
            Container(
              width: 120,
              height: 120,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.1),
                    blurRadius: 20,
                    offset: const Offset(0, 10),
                  ),
                ],
              ),
              child: const Icon(
                Icons.menu_book,
                size: 60,
                color: AppTheme.primaryColor,
              ),
            )
                .animate()
                .scale(
                  duration: AppConstants.mediumAnimation,
                  curve: Curves.elasticOut,
                )
                .fadeIn(duration: AppConstants.shortAnimation),
            
            const SizedBox(height: 32),
            
            // App Name
            Text(
              AppConstants.appName,
              style: Theme.of(context).textTheme.headlineMedium?.copyWith(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                letterSpacing: 1.2,
              ),
            )
                .animate()
                .fadeIn(
                  delay: const Duration(milliseconds: 500),
                  duration: AppConstants.mediumAnimation,
                )
                .slideY(
                  begin: 0.3,
                  end: 0,
                  duration: AppConstants.mediumAnimation,
                  curve: Curves.easeOut,
                ),
            
            const SizedBox(height: 8),
            
            // App Description
            Text(
              'Discover your next favorite book',
              style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                color: Colors.white.withOpacity(0.9),
                letterSpacing: 0.5,
              ),
            )
                .animate()
                .fadeIn(
                  delay: const Duration(milliseconds: 800),
                  duration: AppConstants.mediumAnimation,
                )
                .slideY(
                  begin: 0.3,
                  end: 0,
                  duration: AppConstants.mediumAnimation,
                  curve: Curves.easeOut,
                ),
            
            const SizedBox(height: 48),
            
            // Loading Indicator
            const CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
              strokeWidth: 3,
            )
                .animate()
                .fadeIn(
                  delay: const Duration(milliseconds: 1200),
                  duration: AppConstants.shortAnimation,
                )
                .scale(
                  begin: const Offset(0.8, 0.8),
                  end: const Offset(1.0, 1.0),
                  duration: AppConstants.shortAnimation,
                  curve: Curves.easeOut,
                ),
          ],
        ),
      ),
    );
  }
}
